﻿using SqlSugar;

namespace Git.Storage.WebApi.SqlSugarExtend
{
    public static class SqlSugarExt
    {
        public static void SqlSugarExtSetUp(this IServiceCollection services, IConfiguration configuration)
        {
            //IOC注入：添加业务逻辑注入方式
            services.AddScoped<ISqlSugarClient>(context =>
            {
                string? connectionString = configuration.GetConnectionString("ConnectionString");
                if (string.IsNullOrWhiteSpace(connectionString))
                {
                    throw new Exception("请检查数据库连接字符串配置！！！");
                }
                ConnectionConfig connectionConfig = new ConnectionConfig()
                {
                    ConnectionString = connectionString,
                    DbType = DbType.SqlServer,
                    IsAutoCloseConnection = true
                };
                SqlSugarClient sqlSugarClient = new SqlSugarClient(connectionConfig);
                //配置SqlSugar过滤器 
                {

                }
                //Aop 
                {
                    sqlSugarClient.Aop.OnLogExecuting = (sql, pars) =>
                    {
                        Console.WriteLine(sql);//输出sql,查看执行sql 性能无影响
                        Console.WriteLine("--------------------------------------");

                        //获取原生SQL推荐 5.1.4.63  性能OK
                        //UtilMethods.GetNativeSql(sql,pars)

                        //获取无参数化SQL 对性能有影响，特别大的SQL参数多的，调试使用
                        //UtilMethods.GetSqlString(DbType.SqlServer,sql,pars)


                    };

                }

                return sqlSugarClient;
            });

        }

    }
}